<%@page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@taglib uri="/WEB-INF/utils.tld" prefix="utils"%>
<%@page import="java.util.GregorianCalendar"%>
<%@page import="java.util.ArrayList"%>
<%@page import="fr.unice.CD.calendar.Event"%>
<%@page import="fr.unice.CD.calendar.LoadXML"%>
<%
	//on initialise les var de dates
	String months[] = { "janv.", "fevr.", "mars", "avr.", "mai",
			"juin", "juil.", "aout.", "sept.", "oct.", "nov.", "dec."};
	String jours[] = { "Dimanche", "Lundi", "Mardi", "Mercredi",
			"Jeudi", "Vendredi", "Samedi" };
	GregorianCalendar aujourdhui = null;
	int position, jour, newposition, mois, annee, demainJour, demainMois, demainAnnee, hierJour, hierMois, hierAnnee;
	String dateparam = null;
	int dayOfWeekparam = -1;
	
	//recupere la date passer en URL
	String param = request.getParameter("date");
	if (param != null) {
		position = param.indexOf("\\");
		jour = Integer.parseInt(param.substring(0, position));
		newposition = param.indexOf("\\", position + 1);
		mois = Integer.parseInt(param.substring(position + 1,
				newposition));
		annee = Integer.parseInt(param.substring(newposition + 1));

		//initialise un nouveau calendrier en fonction de la date passer en URL
		aujourdhui = new GregorianCalendar(annee, mois - 1, jour);
		
		dateparam = jour+"/"+(mois-1)+"/"+annee;
		dayOfWeekparam = aujourdhui.get(GregorianCalendar.DAY_OF_WEEK);

		GregorianCalendar demain = (GregorianCalendar) aujourdhui
				.clone();
		demain.add(GregorianCalendar.DATE, +1);
		demainJour = demain.get(GregorianCalendar.DATE);
		demainMois = demain.get(GregorianCalendar.MONTH) + 1;
		demainAnnee = demain.get(GregorianCalendar.YEAR);

		GregorianCalendar hier = (GregorianCalendar) aujourdhui.clone();
		hier.add(GregorianCalendar.DATE, -1);
		hierJour = hier.get(GregorianCalendar.DATE);
		hierMois = hier.get(GregorianCalendar.MONTH) + 1;
		hierAnnee = hier.get(GregorianCalendar.YEAR);
	} else {
		aujourdhui = new GregorianCalendar();
		jour = aujourdhui.get(GregorianCalendar.DATE);
		mois = aujourdhui.get(GregorianCalendar.MONTH);
		annee = aujourdhui.get(GregorianCalendar.YEAR);
		
		dateparam = jour+"/"+(mois)+"/"+annee;
		dayOfWeekparam = aujourdhui.get(GregorianCalendar.DAY_OF_WEEK);

		GregorianCalendar demain = (GregorianCalendar) aujourdhui
				.clone();
		demain.add(GregorianCalendar.DATE, +1);
		demainJour = demain.get(GregorianCalendar.DATE);
		demainMois = demain.get(GregorianCalendar.MONTH) + 1;
		demainAnnee = demain.get(GregorianCalendar.YEAR);

		GregorianCalendar hier = (GregorianCalendar) aujourdhui.clone();
		hier.add(GregorianCalendar.DATE, -1);
		hierJour = hier.get(GregorianCalendar.DATE);
		hierMois = hier.get(GregorianCalendar.MONTH) + 1;
		hierAnnee = hier.get(GregorianCalendar.YEAR);
	}
%>

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>CD-Calendar</title>
<link href="style.css" rel="stylesheet" type="text/css" />
<jsp:useBean id="event" class="fr.unice.CD.calendar.BeanEvent"
	scope="application" />
<%
if(event.getLesEvent().size()==0){
	String path = this.getServletContext().getRealPath(
			request.getContextPath());
	LoadXML.loadXML(path, event);
}
%>
<script type="text/javascript">
		function dessineEvents()
		{
			<%
			//on recupere la date du jour complete sous forme de String
			String DateduJour;
			if(param !=null){
				DateduJour = jour+"/"+(mois)+"/"+annee;
			}else{
				DateduJour = jour+"/"+(mois+1)+"/"+annee;
			}
			//on affiche seulement les evenements dont la date = date du jour
			ArrayList<Event> listeEvents=event.getLesEvent();
			Event evenement;
			for (int i=0;i<listeEvents.size();i++){
				evenement=listeEvents.get(i);
				String dateEvent= evenement.getDateDebut();
				if(dateEvent.equals(DateduJour)){
				String nomEvent = evenement.getNom();
				String heureDebut = evenement.getHeureDebut();
				String duree = evenement.getDuree();
				%>
				var dayOfWeekparam = "<%=dayOfWeekparam%>" ;
				var date = "<%=dateparam%>" ;
				var idEvent = "<%=i%>" ; 
				var heureDebut = "<%=heureDebut%>" ;
				var nomEvent = "<%=nomEvent%>" ;
				var duree = "<%=duree%>" ;
				ajouterEvenement(nomEvent, idEvent, heureDebut, duree, dayOfWeekparam, date);	
				<%
				}//fin IF
			} //fin FOR 
			%>
		}
</script>

<script language="JavaScript" src="script.js"></script>
</head>

<body onMouseMove="detectMouse(event);" onload="dessineEvents()">
<h2>Mon Agenda</h2>
<div id="granularite">
<ul>
	<li><a href="jour.jsp">Jour</a></li>
	<li><a href="semaine.jsp">Semaine</a></li>
	<li><a href="mois.jsp">Mois</a></li>
	<li><a href="planning.jsp">Mon planning</a></li>
</ul>
</div>

<div id="colonnegauche"><a href="ajouter.jsp">Créer un événement</a><br />
<div id="divcalendar"><utils:monCal /></div>
</div>

<div id="colonnedroite">
	<%	
	if (param != null) {
	%> 
		<a href="jour.jsp?date=<%=hierJour+"\\"+hierMois+"\\"+hierAnnee %>"><input 
		type="button" value=" < " class="buttonImg"></a>
		<a href="jour.jsp?date=<%=demainJour+"\\"+demainMois+"\\"+demainAnnee %>"><input 
		type="button" value=" > " class="buttonImg"></a>
		<a href="jour.jsp"><input 
		type="button" value="Aujourd'hui" class="buttonImg"></a> &nbsp;&nbsp; 
		
	<%
 	//affiche la date au format 30 avr. 2008
 		out.println("<b>" + jour + " " + months[mois - 1] + " " + annee
 				+ "</b>");
 	} else {
 	%> 
	 	<a href="jour.jsp?date=<%=hierJour+"\\"+hierMois+"\\"+hierAnnee %>"><input 
	 	type="button" value=" < " class="buttonImg"></a>
		<a href="jour.jsp?date=<%=demainJour+"\\"+demainMois+"\\"+demainAnnee %>"><input 
		type="button" value=" > " class="buttonImg""></a>
		<a href="jour.jsp"><input type="button" value="Aujourd'hui" class="buttonImg"></a> &nbsp;&nbsp; 
	<%
 	out.println("<b>" + jour + " " + months[mois] + " " + annee
 				+ "</b>");
 	}
 	%>
<table id="agendaHead">
	<thead>
		<tr>
			<th border="1" align="center">
			<%
				out.print(jours[aujourdhui.get(GregorianCalendar.DAY_OF_WEEK) - 1]);
			%>
			</th>
		</tr>
	</thead>
</table>

<!-- <div id="overflowAgenda" onscroll="moveElement();"> -->
<div id="overflowAgenda">
<div id="evenements"></div>
<table id="agenda">
	<tbody>
		<%
			int tdid = 1;
			for (int i = 0; i <= 23; i++) {

				out.println("<tr>");
				out.println("<th width=\"7%\" rowspan=\"2\" valign=\"top\">"+ i + ":00</th>");
				out.println("<td  id='td" + tdid+ "' onclick=\"visibilitedivAjout('"+dayOfWeekparam+"','"+dateparam+"', '"+i+":00', "+tdid+");\">&nbsp;</td>");
				out.println("</tr>");
				out.println("<tr>");
				out.println("<td  id='td" + (tdid + 10)+ "' onclick=\"visibilitedivAjout('"+dayOfWeekparam+"','"+dateparam+"', '"+i+":30', "+(tdid+10)+");\">&nbsp;</td>");
				out.println("</tr>");
				tdid += 20;
			}
		%>
	</tbody>
</table>
</div>

	
	<div id="divAjout" style="display: none;">
		<div id="heure_clique"></div><br />
		<form method='get' action='ajouter.jsp'>Objet: <input type="text"
		id="nameEvent" name='nom'><br />
		<input type='hidden' id="date" name='date' value=''>
		<input type='hidden' id="heure" name='heure' value=''> 
		<input type='hidden' id="minutes" name='minutes' value=''> 
		par exemple, projet d'application web<br /><br />
		<input type="submit" value="Crée un événement"></form>
	</div>

	<div id="divModif" style="display: none;"><b>
		<div id="nameEventSelect">nom Event</div>
		<div id='heureClique'></div>
		<form name="supprForm" id="supprForm" method='get' action='Suppression'>
		<input type='hidden' id="idEvent" name='id' value=''> [<a
			href="javascript: submitform('supprForm')">supprimer</a>]<br />
		</form>
		<hr>
		<form name="modifForm" id="modifForm" method='get' action='modifier.jsp'>
		<input type='hidden' id="idEvent2" name='id' value=''> <a
			href="javascript: submitform('modifForm')"> modifier les details de
		l'evenement >></a></form>
	</div>
	
</div><!-- //col de droite -->
	
</body>
</html>
